﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;

namespace WindowsFormsApplication1
{
    public partial class queryForm : Form
    {
        AxMapControl pMapCtrl;
        public IMap pMap;
        IFeatureLayer pFeatureLayer;
        public static int Layerindex = -1;
        public ITable pTable;
        public int n = -1;
        public static int fieldIndex = -1;
        public string fieldname;
        public queryForm(AxMapControl MapCtrl)
        {
            InitializeComponent();
            pMapCtrl = MapCtrl;
            pMap = MapCtrl.Map;
        }

        private void queryForm_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            int layCount = 0;
            layCount = pMap.LayerCount;
            for (int i = 0; i < layCount; i++)
            {
                comboBox1.Items.Add(pMap.get_Layer(i).Name);
            }
        }


        // 查询
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Layerindex = comboBox1.SelectedIndex;
                string filedname = comboBox2.SelectedItem.ToString();
                string value = textBox1.Text;
                pFeatureLayer = pMap.get_Layer(Layerindex) as IFeatureLayer;
                IQueryFilter pQueryFilter = new QueryFilterClass();
                //设置过滤器对象的属性
                pQueryFilter.WhereClause = filedname + " =  '" + value + "'";
                IEnvelope pEnvelope = new EnvelopeClass();
                //执行查询获取符号条件的要素
                List<IFeature> pFList = this.GetSearchFeatures(pFeatureLayer, pQueryFilter);
                if (pFList.Count == 0)
                {
                    MessageBox.Show("暂无数据", "提示");
                    return;
                }

                dataGridView1.RowCount = pFList.Count + 1;
                dataGridView1.ColumnCount = pFList[0].Fields.FieldCount;
                for (int m = 0; m < pFList[0].Fields.FieldCount; m++)
                {
                    dataGridView1.Columns[m].HeaderText = pFList[0].Fields.get_Field(m).AliasName;
                }

                //for (int i = 0; i < pFList.Count; i++)
                //{
                IFeature rFeature = pFList[0];
                for (int j = 0; j < rFeature.Fields.FieldCount; j++)
                {
                    dataGridView1[j, 0].Value = rFeature.get_Value(j).ToString();
                }

                if (rFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint)
                {
                    IPoint pPoint = rFeature.Shape as IPoint;
                    pMapCtrl.CenterAt(pPoint);
                    ICommand pCommand = new ControlsZoomToSelectedCommandClass();
                    pCommand.OnCreate(pMapCtrl.Object);
                    pCommand.OnClick();

                    IEnvelope pEnv = pMapCtrl.Extent;
                    pEnv.Expand(0.2, 0.2, true);
                    pMapCtrl.Extent = pEnv;
                    pMapCtrl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                }
                else
                {
                    IPoint pPoint = new PointClass();
                    pPoint.X = (rFeature.Extent.XMax + rFeature.Extent.XMin) / 2;
                    pPoint.Y = (rFeature.Extent.YMax + rFeature.Extent.YMin) / 2;

                    pMapCtrl.CenterAt(pPoint);
                    ICommand pCommand = new ControlsZoomToSelectedCommandClass();
                    pCommand.OnCreate(pMapCtrl.Object);
                    pCommand.OnClick();

                    IEnvelope pEnv = pMapCtrl.Extent;
                    pEnv.Expand(0.8, 0.8, true);
                    pMapCtrl.Extent = pEnv;
                    pMapCtrl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                }

                pMapCtrl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);

                //}

                pMapCtrl.Refresh();
            }catch(Exception ex){
                MessageBox.Show("请重新选择查询字段");
            }
        }

        // 数据查询
        private List<IFeature> GetSearchFeatures(IFeatureLayer pFeatureLayer, IQueryFilter pQueryFilter)
        {
            try
            {
                List<IFeature> pFeatureList = new List<IFeature>();
                IFeatureCursor pFeatureCursor = pFeatureLayer.Search(pQueryFilter, false);
                IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
                pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
                IFeature pFeature = pFeatureCursor.NextFeature();
                while (pFeature != null)
                {
                    pFeatureList.Add(pFeature);
                    pFeature = pFeatureCursor.NextFeature();
                }
                return pFeatureList;
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message, "异常提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                return null;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (pMap.LayerCount == 0)
            {
                MessageBox.Show("请先加载地图！", "提示");
            }
            else
            {
                Layerindex = comboBox1.SelectedIndex;
                if (Layerindex != -1)
                {
                    IGeoFeatureLayer pGeoFeatureLayer = (IGeoFeatureLayer)pMap.get_Layer(Layerindex);
                    pTable = (ITable)pGeoFeatureLayer;
                    int num1 = pTable.Fields.FieldCount;
                    comboBox2.Items.Clear();
                    for (int i = 0; i < num1; i++)
                    {
                        IField item = pTable.Fields.get_Field(i);
                        if (item.Name.ToString() == "Shape" || item.Name.ToString() == "SHAPE")
                        {
                            n = i;
                            continue;
                        }
                        comboBox2.Items.Add(item.Name.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("请先选择图层！", "提示");
                }
            }
        }
    }
}
